﻿#ifndef HTTP_CONTEXT_H
#define HTTP_CONTEXT_H

#include "httprequest.h"

// 读取http的上下文
enum class HttpRecvStatu
{
    RECV_REQUEST_ERROR,
    RECV_REQUEST_LINE,
    RECV_REQUEST_HEAD,
    RECV_REQUEST_BODY,
    RECV_REQUEST_OVER
};

class Buffer;


#define MAX_LINE 8192
class HttpContext
{
private:
    int m_respStatu;           // 响应状态码
    HttpRecvStatu m_recvStatu; // 当前接收及解析的阶段状态
    HttpRequest m_request;      // 已经解析得到的请求信息
private:
    bool parseRequestLine(const std::string &line);
    bool recvRequestLine(Buffer *buf);
    // 解析头部
    bool parseRequestHead(std::string &line);
    bool recvRequestHead(Buffer *buf);
    // 解析正文
    bool recvRequestBody(Buffer *buf);
public:
    HttpContext();
    void reset();
    int respStatu();
    HttpRecvStatu recvStatu();
    HttpRequest &request();
    // 接收并解析HTTP请求
    bool recvHttpRequest(Buffer *buf);
};

#endif

